//
//  VideoChatedViewController.m
//  模拟-百思不得姐
//
//  Created by 程春玉 on 16/8/17.
//  Copyright © 2016年 colin. All rights reserved.
//

#import "VideoChatedViewController.h"


@interface VideoChatedViewController ()

@end

@implementation VideoChatedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _session.remoteView = [[EMCallRemoteView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    [self.view addSubview:_session.remoteView];
    //2.自己窗口
    CGFloat width = 80;
    CGFloat height = self.view.frame.size.height / self.view.frame.size.width * width;
    _session.localView = [[EMCallLocalView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [self.view addSubview:_session.localView];
    
    
    
        // Do any additional setup after loading the view.
}

- (void)createBackBtn
{
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"Profile_AddV_authen"] forState:(UIControlStateNormal)];
    btn.frame = CGRectMake(0, 0, 40, 40);
    btn.x = self.view.center.x;
    btn.y = self.view.height - 70;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:btn];
    
}

- (void)btnClick
{
    UIAlertController *al = [UIAlertController alertControllerWithTitle:nil message:@"确定关闭视频通话吗" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [[EMClient sharedClient].callManager endCall:_session.sessionId reason:(EMCallEndReasonHangup)];
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    [al addAction:sure];
    [al addAction:cancel];
    [self presentViewController:al animated:YES completion:nil];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    UIAlertController *al = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"%@ 想和您进行视频通话，是否同意？", _session.remoteUsername] preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"同意" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [[EMClient sharedClient].callManager answerCall:_session.sessionId];
        [self createBackBtn];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"不同意" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        [[EMClient sharedClient].callManager endCall:_session.sessionId reason:EMCallEndReasonDecline];
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
    [al addAction:sure];
    [al addAction:cancel];
    [self presentViewController:al animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
